// pages/login/login.js
const app = getApp();
Page({

  data: {
    account: '',
    password: '',
    captchaImage: '',
    captchaText: '',
    isLoading: false,
    uuid: ''
  },

  onAccountInput(e) {
    this.setData({ account: e.detail.value });
  },

  onPasswordInput(e) {
    this.setData({ password: e.detail.value });
  },

  onCaptchaInput(e) {
    this.setData({ captchaText: e.detail.value });
  },

  // 获取验证码图片
  getCode() {
    this.setData({ isLoading: true });
    wx.request({
      url: 'http://localhost:8080/captchaImage',
      method: 'GET',
      success: (res) => {
        console.log(res.data);
        if (res.statusCode === 200) {
          // 构建图片URL
          const imageUrl = 'data:image/png;base64,' + res.data.img;
          this.setData({ 
            captchaImage: imageUrl,
            uuid: res.data.uuid
           });
        } else {
          console.error('获取验证码失败:', res);
          wx.showToast({
            title: '获取验证码失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        console.error('请求出错:', err);
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ isLoading: false });
      }
    });
  },

  // 刷新验证码
  refreshCaptcha() {
    this.getCode();
  },

  // 登录处理
  handleLogin() {
    const { account, password, captchaText } = this.data;
    if (!account || !password || !captchaText) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }else {
      const username = this.data.account
      const password = this.data.password
      const code = this.data.captchaText
      const uuid = this.data.uuid
      console.log(username,password,code,uuid);
      wx.request({
      url: 'http://localhost:8080/login',
      method: 'POST',
      data: {
        username,
        password,
        code,
        uuid
      },
      success: (res) => {
        console.log(res.data);
        if(res.data.code == 200){
          wx.hideLoading();
          wx.showToast({ title: '登录成功' });
          app.globalData.token = res.data.token

          wx.request({
            url: 'http://localhost:8080/getInfo',
            header: {
              Authorization: app.globalData.token,
            },
            success:(res)=>{
              if(res.data.code === 200){
                app.globalData.userInfo = res.data.user
               
              }
            }
          })

            
      // 登录成功
      wx.showToast({
        title: '登录成功',
        icon: 'success',
        duration: 1000
      });

      // 延迟一秒跳转首页
      setTimeout(() => {
        wx.switchTab({ 
          url: '/pages/index/index'
        });
      }, 1000);
        }else {
          wx.hideLoading();
          wx.showToast({ 
            icon: 'error', 
            title: res.data.msg,
            duration: 2000 
         });
         this.getCode()
        }
        // console.log(app.globalData.token);
      }
      })
    }
    // 此处添加登录逻辑（如调用接口）
    // wx.showLoading({ title: '登录中...' });
    // // 模拟登录请求
    // setTimeout(() => {
    //   wx.hideLoading();
    //   wx.showToast({ title: '登录成功' });
    //   // 跳转至首页
    //   wx.navigateTo({ url: '/pages/index/index' });
    // }, 1500);
  },
  
  onLoad(options) {
    this.getCode();
  },

  stepRegister(){
    wx.navigateTo({
      url: '/pages/register/register',
    })
  }
})